<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends Controller {
    //博文动态验证规则
    public $rules = array(
        array('article_title','require','文章标题不能为空',1),
        array('article_content','require','文章内容不能为空',1),
    );

    /**
     * 自动验证
     * @param  Object $modesName 模型名称
     * @return 成功boolea,失败提示信息     
     */
    private function autoVerify($modesName){
        if ($modesName->validate($this->rules)->create($articleData)) {
           return true;
        } else {
          $this->error($modesName->getError());
        }
    }

    /**
     * 上传博文图片
     * @return string 成功返回图片存储路径,失败给与提示
     */
    private function updateArticleImg(){
        $result = upload(C('ARTICLE_IMG_FILENAME'));
        if ($result['state'] === 'success') {
            return $result['filePath'];
        } else {
            $this->error($result['message']);
        }
    }

    /**
     * 博文首页
     */
    public function index(){
        $fieldString = array(
            'article_id',
            'article_title',
            'article_img',
            'article_time',
            'sort',
            'click',
            'is_display',
        );
        $articleList = D('Article')->selectArticle($fieldString);
        $this->assign('articleList',$articleList);
        $this->display('article');
    }

    /**
     * 添加博文
     */
    public function articleAdd(){
        if (IS_POST) {
            $Article = D('Article');
            $articleData = I('post.');
            //自动验证
            $this->autoVerify($Article);
            //上传文件
            if (!empty($_FILES['article_img']['size'])) {
               $articleData['article_img'] = $this->updateArticleImg();
            }
            //生成文章摘要
            $articleDigest = $this->articleDigest($articleData['article_content']);
            $articleData['article_digest'] = $articleDigest;
            $articleData['article_time'] = time();
            $articleData['admin_user_admin_id'] = 1;
            if ($Article->insertArticle($articleData)) {
                $this->success('博文添加成功');
            } else {
                $this->error('博文添加失败');
            }
        } else {
            $this->display('articleAdd');
        }
    }

    /**
     * 修改博文
     */
    public function articleEditor(){
       $Article = D('Article');
       $fieldString = array(
            'article_id',
            'article_title',
            'article_digest',
            'article_img',
            'sort',
            'is_display',
            'article_content',
        );
    	if (IS_POST) {
            $articleData = I('post.');
            $articleData['is_display'] = array_key_exists('is_display',$articleData) ? 1 : 0;
            //自动验证
            $this->autoVerify($Article);
            //上传文件
            if (!empty($_FILES['article_img']['size'])) {
               $articleData['article_img'] = $this->updateArticleImg();
            }
            //生成文章摘要
            $articleDigest = $this->articleDigest($articleData['article_content']);
            $articleData['article_digest'] = $articleDigest;
            $wheres['article_id'] = $articleData['aid'];
            $id = $Article->updateArticle($wheres,$fieldString,$articleData);
            if ($id) {
                $this->success('博文修改成功');
            } else {
                $this->error('博文修改失败');
            }
            
    	} else {
            $articleId = I('get.aid');
            $articleData = $Article->selectOneArticle($fieldString,['article_id' => $articleId]);
            $this->assign('articleData',$articleData);
    		$this->display('articleEditor');
    	}
    }

    /**
     * 删除博文
     */
    public function articleDel(){
        $articleId = I('get.aid');
        $id = D('Article')->deleteArticle(['article_id' => $articleId]);
        if ($id) {
            $this->success('博文删除成功');
        } else {
            $this->error('博文删除失败');
        }
    }

    /**
     * 生成文章摘要
     * @param  string $content 文章内容
     * @return string          文章摘要
     */
    protected function articleDigest($content){
        $content =strip_tags(htmlspecialchars_decode($content));
        return mb_substr($content,0,mt_rand(100,150)).'...';
    }


}